package com.weitu.permission.userRole.service.impl;

import com.weitu.permission.user.entity.UserRole;
import com.weitu.permission.userRole.mapper.UserRoleMapper;
import com.weitu.permission.userRole.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author LiMeng 2017/3/21
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {

    private final UserRoleMapper userRoleMapper;

    @Autowired
    public UserRoleServiceImpl(UserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    @Transactional
    public boolean deleteUserRoleByUserId(String userId) {
        int count = userRoleMapper.deleteUserRoleByUserId(userId);
        return count > 0;
    }

    @Override
    @Transactional
    public boolean insertUserRole(List<UserRole> userRoleList) {
        int count = userRoleList.size();
        int result = userRoleMapper.insertUserRole(userRoleList);
        return count == result;
    }

    @Override
    @Transactional
    public boolean updateUserRole(String userId, List<UserRole> userRoleList) {
        userRoleMapper.deleteUserRoleByUserId(userId);
        int j = userRoleMapper.insertUserRole(userRoleList);
        return j == userRoleList.size();
    }
}
